package org.future.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;


public class RefactFile {
    public static void main(String[] args) {
        String folder = "D:/eclipse3.5/workspace/loushang-srr/src/org/loushang/srr/wsdl2/utils";
        String suffix = ".java";
        String oldVal = ".future.wsdl.";
        String newVal = ".loushang.srr.wsdl2.";
        
        RefactFile t = new RefactFile();
        t.refact(folder, suffix, oldVal, newVal);
    }
    public void refact(String folder,String suffix,
            String oldVal,String newVal){
        List<String> list = getAllFiles(folder, suffix);
        for(int i=0;i<list.size();i++){
            String path = list.get(i);             // 文件路径
            String temppath = path + ".temp";      // 临时文件路径
            modifyFile(path, temppath,oldVal,newVal);
            File oldFile = new File(path);
            File tempFile = new File(temppath);
            oldFile.delete();                      // 删除旧文件
            boolean result = tempFile.renameTo(oldFile);  // 将临时文件重名为旧文件
            System.out.println((i + 1) + ": refact file -> " + path + " [" + result + "]");
        }
        System.out.println("size:" + list.size());
    }
    /**
     * 返回目录 dir 及所有子目录中以 suffix 结尾的 文件
     * @param dir :String
     * @param suffix :String
     * @return List<String> fileAbsolutePath
     */
    public List<String> getAllFiles(String dir,String suffix){
        List<String> list = new ArrayList<String>();
        File file = new File(dir);
        String filename = file.getName().toLowerCase(); // 文件名转小写
        if(file.isFile() && (filename.endsWith(suffix))){
            list.add(file.getAbsolutePath());           // 保存符合条件的文件路径
        } else if(file.isDirectory()){
            File[] fs = file.listFiles();
            for(File f : fs){                           // 遍历当前目录
                getAllFiles(f,suffix,list);             // 递归遍历子目录
            }
        }
        return list;
    } 
    /**
     * 取出目录 dir 及所有子目录中以 suffix 结尾的 文件
     * @param file :File
     * @param suffix :String
     * @param list :List<String>
     */
    private void getAllFiles(File file,String suffix,List<String> list){
        String filename = file.getName().toLowerCase(); // 文件名转小写
        if(file.isFile() && (filename.endsWith(suffix))){
            list.add(file.getAbsolutePath());           // 保存符合条件的文件路径
        } else if(file.isDirectory()){
            File[] fs = file.listFiles();
            for(File f : fs){                           // 遍历当前目录
                getAllFiles(f,suffix,list);             // 递归遍历子目录
            }
        }
    }// end
    /**
     * 复制文件
     * @param src :String fileName
     * @param dest :String fileName
     * @throws Exception
     */
    public void modifyFile(String src,String dest,
            String oldVal,String newVal){        
        BufferedReader in = null;
        PrintWriter out = null;
        try{
            in = new BufferedReader(new FileReader(src));;
            out = new PrintWriter(dest);
            String data = in.readLine();
            while(data != null){             // 按行读取
                String temp = data.trim();
                // update text
                if(temp.indexOf(oldVal) > 0 && (temp.startsWith("package") || temp.startsWith("import"))){
                    data = data.replace(oldVal, newVal);
                }
                out.print(data);
                out.println();               // 换行
                data = in.readLine();
            }
        } catch(IOException e){
            e.printStackTrace();
        } finally{
            try{
                if(in != null){
                    in.close();
                }
                if(out != null){
                    out.flush();
                    out.close();
                }
            } catch(Exception e){
                e.printStackTrace();
            }
        }
    } 
    
}
